#通讯录页面
import datetime
import os.path
import time

from selenium.webdriver.common.by import By
from auto_frame.base import Base
from data_file_analysis.yaml_analysis import YamlAnalysis as ya
from logs.logger import Logger

class ContactPage(Base):
    contact_url = 'https://work.weixin.qq.com/wework_admin/frame#contacts'
    ele_add_member = (By.CSS_SELECTOR, "a.qui_btn.ww_btn.js_add_member:nth-child(2)")
    ele_add_department = (By.CSS_SELECTOR, '.js_add_sub_party')
    ele_research = (By.ID, 'memberSearchInput')
    ele_research_result = (By.CSS_SELECTOR, '#search_party_list > li > a')
    cur_path = os.path.abspath(os.path.dirname(__file__))  # 获取当前文件目录
    root_path = os.path.dirname(cur_path)
    time_now = datetime.datetime.now()
    time_now1 = f'{str(time_now.year)}_{str(time_now.month)}_{str(time_now.day)}_{str(time_now.hour)}_{str(time_now.minute)}'
    log = Logger(f'{root_path}/logs/info/{time_now1}_info.log', level='info')

    # 初始化driver
    def __init__(self, driver):
        super(ContactPage, self).__init__(driver)
        cur_path = os.path.abspath(os.path.dirname(__file__))  # 获取当前文件目录
        self.root_path = os.path.dirname(cur_path)
        self.step_path = f'{self.root_path}/steps/contact_page.yaml'

    #检查是否成功添加成员
    def check_add_member(self):
        '''
        1.获取总页数，判断当前页面在第几页
        2.如果不是最后一页，则先获取当前页面所有电话号码，并将电话号码加入电话列表中
        3.当前页面电话号码加入到列表中,再翻页继续添加
        :return:
        '''
        mobile_phone_list = []
        eles = ya.return_yaml_data(self.step_path, 'check_add_member')
        self.log.logger.info("check_add_member:")

        while True:
            time.sleep(3)

            self.log.logger.info(f"by_type:{eles[0]['by']},locator:{eles[0]['locator']}")
            ele_text = str(self.driver.find_element(eles[0]['by'], eles[0]['locator']).text)
            page_nums = ele_text.split('/')
            cur_page_num = int(page_nums[0])
            total_page_num = int(page_nums[1])

            self.log.logger.info(f"by_type:{eles[1]['by']},locator:{eles[1]['locator']}")
            mobile_nums = self.driver.find_elements(eles[1]['by'], eles[1]['locator'])

            for i in mobile_nums:
                mobile_phone_list.append(i.text)

            if cur_page_num != total_page_num:

                self.log.logger.info(f"by_type:{eles[2]['by']},locator:{eles[2]['locator']}")
                self.ele_click(eles[2]['by'], eles[2]['locator'])

            else:
                break

        return mobile_phone_list

    #编辑成员
    def edit_member_op(self):
        from member_operation import MemberOperation
        return MemberOperation(self.driver)

    #添加部门
    def add_department(self):
        pass

    #添加子部门
    def add_sub_department(self, par_department):
        pass

    #检查是否成功添加部门,通过查找部门
    def check_add_department(self, department):
        '''
        sleep(4)
        research = find_element(self.driver, self.ele_research)
        research.click()
        action = ActionChains(self.driver)
        action.send_keys(department).pause(1)
        action.perform()
        research_result = find_element(self.driver, self.ele_research_result)
        #print(research_result.text)
        return research_result.text
        '''

    #跳转到导入通讯录页面
    def goto_import_contact(self):
        '''
        from page.import_cantact_page import ImportContactPage
        return ImportContactPage(self.driver)
        '''
        pass

